/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>
#include <vector>
#include <memory>
#include <functional>
#include <optional>
#include "nlohmann/json.hpp"
#include "utils_noncopyable.h"

namespace El {
namespace FileManager {

// 文件类型枚举
enum class FileType { AUDIO, IMAGE, VIDEO, ALL };

// 文件信息结构
struct FileInfo {
    std::string id;      // 文件唯一标识
    std::string name;    // 文件名
    std::string type;    // 文件类型（audio/image/video）
    std::string path;    // 文件路径
    int32_t size;        // 文件大小
    bool is_deleted;     // 虚拟删除状态
    int64_t last_access; // 最后访问时间
    std::string origin;  // 文件来源 ("preset" or "user")

    // 转换为JSON
    nlohmann::json ToJson() const;

    // 从JSON解析
    static std::optional<FileInfo> FromJson(const nlohmann::json &json);
};

// Interface class
class IFileManager {
public:
    // Virtual destructor is crucial for interfaces
    virtual ~IFileManager() = default;

    // 获取单例 (returns interface reference)
    static IFileManager &GetInstance();

    // 初始化文件管理系统
    virtual bool Start() = 0;

    // 获取文件列表
    virtual std::vector<FileInfo> GetFileList(FileType type = FileType::ALL, bool includeDeleted = false) = 0;

    // 获取单个文件信息
    virtual bool GetFileInfo(const std::string &fileId, FileInfo &outInfo) = 0;

    // 通过文件名获取文件信息
    virtual bool GetFileInfoByName(const std::string &fileName, FileInfo &outInfo) = 0;

    // 删除文件（预制文件虚拟删除，用户文件实际删除）
    virtual bool DeleteFile(const std::string &fileId) = 0;

    // 恢复虚拟删除的预制文件
    virtual bool RestoreFile(const std::string &fileId) = 0;

    // 创建新文件（准备写入）
    virtual std::string CreateFile(const std::string &fileName, FileType type) = 0;

    // 分块写入文件
    virtual bool WriteFileBlock(const std::string &fileId, const std::string &block, bool isLastBlock) = 0;

    // 获取存储空间信息
    virtual bool GetStorageInfo(int32_t &totalSpace, int32_t &usedSpace, int32_t &availableSpace) = 0;

    // 返回文件实际路径
    virtual std::string GetFilePath(const std::string &fileId) = 0;

    // 分块读取文件
    virtual bool ReadFileBlock(const std::string &fileId, int chunkIndex, int32_t chunkSize, std::string &outData) = 0;

protected:
    // Protected constructor/destructor if needed, but usually not for pure interfaces
    IFileManager() = default;

private:
    // Prevent copying/moving the interface itself if desired, though often not necessary for pure interfaces
    // DISALLOW_COPY_AND_MOVE(IFileManager); // Optional
};

} // namespace FileManager
} // namespace El